package com.stgl.util;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import org.apache.logging.log4j.LogManager;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

public class sendmessage {


    public static boolean sendMsg(String phone,  String templateParam) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIOn0gT3xaTGmc", "JOzW0WiM7wA13C6oBadv8sVyFrDq5h");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setAction("SendSms");
        request.setVersion("2017-05-25");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("SignName", "柒彩科技");
        request.putQueryParameter("TemplateCode", "SMS_173230364");

        request.putQueryParameter("TemplateParam",templateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String datas=response.getData();
            Map<String,Object>maps1= JSON.parseObject(datas);
            if(maps1.get("Code")!=null){
                if(maps1.get("Code").toString().equals("OK")){
                    return true;
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }





    public static boolean sendMsg1(String phone,  String templateParam) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIOn0gT3xaTGmc", "JOzW0WiM7wA13C6oBadv8sVyFrDq5h");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setAction("SendSms");
        request.setVersion("2017-05-25");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("SignName", "柒彩科技");
        request.putQueryParameter("TemplateCode", "SMS_173251020");
        request.putQueryParameter("TemplateParam",templateParam);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String datas=response.getData();
            Map<String,Object>maps1= JSON.parseObject(datas);
            if(maps1.get("Code")!=null){
                if(maps1.get("Code").toString().equals("OK")){
                    return true;
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }


    /**
     * 递归读取文件路径下的所有文件
     *
     * @param path
     * @param fileNameList
     * @return
     */
    public static ArrayList<String> readFiles1(String path, ArrayList<String> fileNameList) {
        File file = new File(path);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    readFiles1(files[i].getPath(), fileNameList);
                } else {
                    String path1 = files[i].getPath();
                    String fileName = path1.substring(path1.lastIndexOf("\\") + 1);
                    fileNameList.add(fileName);
                }
            }
        } else {
            String path1 = file.getPath();
            String fileName = path1.substring(path1.lastIndexOf("\\") + 1);
            fileNameList.add(fileName);
        }
        return fileNameList;

    }



    //3.添加定时任务
    //@Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：1800秒
    @Scheduled(fixedRate=1800000)
    private static void configureTasks() {
        System.err.println("执行静态定时任务时间: " + LocalDateTime.now());
    }


    public static void main(String[] args) {
        /*DefaultProfile profile = DefaultProfile.getProfile("default", "LTAIOn0gT3xaTGmc", "JOzW0WiM7wA13C6oBadv8sVyFrDq5h");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setAction("SendSms");
        request.setVersion("2017-05-25");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", "13027850003");
        request.putQueryParameter("SignName", "柒彩科技");
        request.putQueryParameter("TemplateCode", "SMS_173251020");


        String param="{\"sname\":\"Tom\", \"csname\":\"播州区直属机关幼儿园\", \"sbyy\":\"没上传图片\"}";
        request.putQueryParameter("TemplateParam",param);
        try {
            CommonResponse response = client.getCommonResponse(request);
            String datas=response.getData();
            Map<String,Object>maps1= JSON.parseObject(datas);
            System.out.println(maps1);
            System.out.println(maps1.get("Code"));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
*/
        configureTasks();

    }


}
